package com.googlecode.perfectable.visuable.visual;

import java.util.List;
import java.util.UUID;

import com.google.common.base.Supplier;
import com.googlecode.perfectable.visuable.renderer.ElementRenderer;
import com.googlecode.perfectable.visuable.structure.Attribute;

public final class IdentifiedVisuals {
	private static final String IDENTIFIER_ATTRIBUTE_NAME = "id";
	private static final String IDENTIFIER_PREFIX = "px-";

	private static final Supplier<String> IDENTIFIER_SUPPLIER = new Supplier<String>() {
		@Override
		public String get() {
			return IDENTIFIER_PREFIX + UUID.randomUUID().toString();
		}
	};

	public static String extractIdentifier(List<Attribute> elementAttributes) {
		return Attribute.find(elementAttributes, IDENTIFIER_ATTRIBUTE_NAME).or(IDENTIFIER_SUPPLIER);
	}

	private IdentifiedVisuals() {
		// prevents initialization
	}

	public static void render(ElementRenderer painter, IdentifiedVisual visual) {
		String identifier = visual.identifier();
		painter.attribute(IDENTIFIER_ATTRIBUTE_NAME, identifier);
	}

}
